package com.zb.web.controller;

import com.zb.model.TReceiveArea;
import com.zb.service.ReceiveAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/receive")
public class ReceiveAreaController {

	@Autowired
	private ReceiveAreaService ls;
	
	/**
	 *  ��ѯ���˺ŵ��ռ���ַ
	 * @param userid
	 * @param session
	 * @return
	 */
	@RequestMapping("/selectArea")
	public String selectArea(String userid,HttpSession session) {
		System.out.println("----------��ʼ����");
		userid=session.getAttribute("userid").toString();
		List<TReceiveArea> areaList=ls.selectAreaService(userid);
		System.out.println("----------��������");
		if(areaList!=null) {
			session.setAttribute("areaList", areaList);
			return "udai_address";
		}else {
			return"udai_address";
		}
			
	}
	
	/**
	 * ɾ��ĳ��ַ
	 * @param areaid
	 * @return
	 */
	@RequestMapping("/deleteArea")
	public String deleteArea(String areaid) {
		boolean flag=ls.deleteAreaService(areaid);
		System.out.println("ִ����ɾ������");
		if(flag==false) {
			System.out.println("shibai");
			JOptionPane.showMessageDialog(null, "delete false");
			System.out.println("shibai----");
			return "udai_address";
		}else {
			System.out.println("chenggong");
			JOptionPane.showMessageDialog(null, "delete success");
			System.out.println("chenggong=-----");
			return"udai_address";
		}
			
	}
	
	/**
	 * ����ջ���ַ
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/addarea")
	public String addarea(HttpSession session,HttpServletRequest request) {
		TReceiveArea treceive = new TReceiveArea();
		String areaid=UUID.randomUUID().toString();
		String userid=session.getAttribute("userid").toString();
		String shoujianren=request.getParameter("name");
		String suozaidi=request.getParameter("suozaidi");
		String xiangxi=request.getParameter("xiangxi");
		String shoujianphone=request.getParameter("mobile");
		boolean flag=ls.addAreaService(areaid,userid,shoujianren,suozaidi,xiangxi,shoujianphone);
		if(flag==false) {
			JOptionPane.showMessageDialog(null, "add false");
			//response.sendRedirect("Login.jsp");
			return "udai_address";
		}else {
			JOptionPane.showMessageDialog(null, "add ok");
			return"udai_address";
		}
			
	}
	
	
	/**
	 * �޸��ջ���ַ
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/updatearea")
	public String updatearea(HttpSession session,HttpServletRequest request) {
		String areaid=request.getParameter("areaid");
		String userid=session.getAttribute("userid").toString();
		String shoujianren=request.getParameter("name");
		String suozaidi=request.getParameter("suozaidi");
		String xiangxi=request.getParameter("xiangxi");
		String shoujianphone=request.getParameter("mobile");
		boolean flag=ls.updateAreaService(areaid,userid,shoujianren,suozaidi,xiangxi,shoujianphone);
		if(flag==false) {
			JOptionPane.showMessageDialog(null, "modify false");
			//response.sendRedirect("Login.jsp");
			return "udai_address";
		}else {
			JOptionPane.showMessageDialog(null, "modify ok");
			return"udai_address";
		}
			
	}
}
